/*****************************************************************
 *   Licensed to the Apache Software Foundation (ASF) under one
 *  or more contributor license agreements.  See the NOTICE file
 *  distributed with this work for additional information
 *  regarding copyright ownership.  The ASF licenses this file
 *  to you under the Apache License, Version 2.0 (the
 *  "License"); you may not use this file except in compliance
 *  with the License.  You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing,
 *  software distributed under the License is distributed on an
 *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 *  KIND, either express or implied.  See the License for the
 *  specific language governing permissions and limitations
 *  under the License.
 ****************************************************************/
package org.epiphron.hermit;

import java.util.Set;

import org.epiphron.BasicModelValidator;
import org.semanticweb.HermiT.Reasoner;
import org.semanticweb.HermiT.Reasoner.ReasonerFactory;
import org.semanticweb.owlapi.model.OWLAxiom;
import org.semanticweb.owlapi.model.OWLOntology;
import org.semanticweb.owlapi.reasoner.OWLReasoner;
import org.semanticweb.owlapi.reasoner.OWLReasonerFactory;

import com.clarkparsia.owlapi.explanation.BlackBoxExplanation;
import com.clarkparsia.owlapi.explanation.HSTExplanationGenerator;

/**
 * Validator of model with HermiT reasoner
 * @see http://hermit-reasoner.com/
 */
class HermiTModelValidator extends BasicModelValidator {
	protected OWLReasonerFactory createReasonerFactory(boolean useUNA) {
		ReasonerFactory factory = new Reasoner.ReasonerFactory() {
            protected OWLReasoner createHermiTOWLReasoner(org.semanticweb.HermiT.Configuration configuration,OWLOntology ontology) {
                // don't throw an exception since otherwise we cannot compte explanations 
                configuration.throwInconsistentOntologyException = false;
                return new Reasoner(configuration,ontology);
            }  
        };
		
		return factory;
	}
	
	@Override
	protected String[] getExplanations(OWLOntology ontology,
			OWLReasonerFactory factory, OWLReasoner reasoner) {
		BlackBoxExplanation exp=new BlackBoxExplanation(ontology, factory, reasoner);
        HSTExplanationGenerator multExplanator = new HSTExplanationGenerator(exp);

        Set<Set<OWLAxiom>> explanations = multExplanator.getExplanations(
        		ontology.getOWLOntologyManager().getOWLDataFactory().getOWLThing());
        String[] expls = new String[explanations.size()];
		
        int i = 0;
        for (Set<OWLAxiom> explanation : explanations) {
        	// building description for every axiom
        	StringBuilder builder = new StringBuilder("Axioms causing the inconsistency:");
        	
            for (OWLAxiom causingAxiom : explanation) {
            	builder.append("\n  ").append(causingAxiom);
            }
            expls[i++] = builder.toString();
        }
        
        return expls;
	}
}
